

BlueTooth: (options) ->
  @options = options
  @isOpen = false;
  @_discoveryStarted = false;
  @connected = false;
  @options =
    device: "unknow"
    deviceId: "unknow"
    deviceName: "unknow"
    deviceAddress: "unknow"
    deviceType: "unknow"
    characteristicUUID: "unknow"
    serviceUUID: "unknow"
    loadingTips: (res = {type: 1, msg: "加载中"})->
      console.log("loadingTips", type, msg)
  @initBlues(options);
BlueTooth.prototype.initBlues=(options)->
  @options = options
  @
BlueTooth.prototype.startScan = ()->
  that = @
  if @isOpen
    that.getBluetoothAdapterState()
  else
    that.openBluetoothAdapter()
  return @

BlueTooth.prototype.startBluetoothDevicesDiscovery= ()->
  that = @
  return if @_discoveryStarted
  @_discoveryStarted = true
  #    app.showLoading '正在搜索设备...'
  @options.loadingTips({type: 1, msg: '正在搜索设备...'})
  setTimeout ()->
    that.stopBluetoothDevicesDiscovery()
  , 3000
  wx.startBluetoothDevicesDiscovery
    services: [that.options.serviceUUID]
    powerLevel: 'high',
    allowDuplicatesKey: false,
    success: ()->
      that.onBluetoothDeviceFound()
#        that.getBluetoothDevices()
BlueTooth.prototype.openBluetoothAdapter=()->
  that = @
  wx.openBluetoothAdapter
    success: (res)->
      that.isOpen = true
      that.getBluetoothAdapterState()
    fail: (res)->
      that.isOpen = false
      that.options.loadingTips({type: 2, msg: '蓝牙适配器不可用'})
  #          reject({msg: "蓝牙适配器不可用"})
  wx.onBluetoothAdapterStateChange (res)->
    if res.available
      that.getBluetoothAdapterState()
    else
      that.stopBluetoothDevicesDiscovery()
      that.devices = [];
      that.options.loadingTips({type: 2, msg: '蓝牙开关未打开'})
#        reject({msg: "蓝牙开关未打开"})
BlueTooth.prototype.closeBluetoothAdapter=()->
  wx.closeBluetoothAdapter()
  @_discoveryStarted = false
BlueTooth.prototype.getBluetoothAdapterState= ()->
  that = @
  wx.getBluetoothAdapterState
    success: (res) ->
      isDiscov = res.discovering #是否正在搜索设备
      isDvailable = res.available #蓝牙适配器是否可用
      that.startBluetoothDevicesDiscovery() if isDvailable and !isDiscov

BlueTooth.prototype.onBluetoothDeviceFound= ()->
  that = @
  wx.onBluetoothDeviceFound (res)->
    for device in res.devices
      if device.name == that.options.deviceName
        that.createBLEConnection(device.deviceId)
        break
BlueTooth.prototype.createBLEConnection= (e) ->
  that = @
  ds = e.currentTarget.dataset
  devId = ds.deviceId #设备UUID
  name = ds.name #设备名
  that.stopConnectDevices() #配对之前先断开已连接设备
  wx.createBLEConnection
    deviceId: devId
    success: (res)->
      that.connected = true
      that.options.device_id = devId
      that.name = name
      that.getBLEDeviceServices(devId)
    fail: (res)->
      that.data.connected = false
      if res.errCode == 10002
        that.options.loadingTips({type: 2, msg: '链接超时，请重试'})
      else if res.errCode == 10003
        that.options.loadingTips({type: 2, msg: '链接蓝牙失败，蓝牙地址无效'})
      else
        that.options.loadingTips({type: 2, msg: '链接超时，请重试'})
      that.closeBLEConnection()
  that.stopBluetoothDevicesDiscovery() #停止搜索
BlueTooth.prototype.closeBLEConnection= ()->
  that = @
  wx.closeBLEConnection
    deviceId: that.data.deviceId
module.exports =
  BlueTooth: BlueTooth